<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/30
 * Time: 17:45
 */

namespace app\admin\controller;
use app\common\logic\Order;
use app\common\model\OrderRecord;
use app\common\model\RoomBackground;
use app\common\model\RoomCategory;
use app\common\model\Room;

use Qiniu\Auth;
use think\Db;
use app\common\model\Music;
class Play extends Base
{

    public static $room_type = [
//        1=>娱乐 2=>电台  3=>派单房 4=>扭蛋 5=>游戏 6=>点唱
        '1' =>['type_name'=>'娱乐','type'=>'1'],
        '2' =>['type_name'=>'电台','type'=>'2'],
        '3' =>['type_name'=>'派单房','type'=>'3'],
        '4' =>['type_name'=>'游戏','type'=>'4'],
        '5' =>['type_name'=>'情感','type'=>'5'],
        '6' =>['type_name'=>'点唱','type'=>'6'],
    ];


    /**
     * 直播列表
     */
    public function index()
    {
        $where = [];
//        $where['is_order'] = 1;
        if (!empty($_GET['room_name'])) $where['room_name'] = ['like','%'.trim(input('get.room_name')).'%'];
        if (!isEmpty($_GET['status'])) $where['status'] = input('get.status');
        if (!isEmpty($_GET['type'])) $where['type'] = input('get.type');
        if (is_numeric($_GET['is_order'])) $where['is_order'] = input('get.is_order');
        if (is_numeric($_GET['top'])) $where['top'] = input('get.top');
        if (is_numeric($_GET['room_id'])) $where['room_id|show_id'] = input('get.room_id');
        $model  = new Room();
        $rows = $model->getList($where);
        $this->assign([
            'rows' => $rows,
            'title' => '直播列表',
            'pageHTML' => $rows->render(),
            'type' => self::$room_type,
        ]);
        return view();

    }

    /**
     * 直播列表
     */
    public function order_index()
    {
        $where['is_order'] = ['in',[2,3]];
        if (!empty($_GET['room_name'])) $where['room_name'] = ['like','%'.trim(input('get.room_name')).'%'];
        if (!isEmpty($_GET['status'])) $where['status'] = input('get.status');
        if (!isEmpty($_GET['type'])) $where['type'] = input('get.type');
        if (!isEmpty($_GET['is_order'])) $where['is_order'] = input('get.is_order');
        $model  = new Room();
        $rows = $model->getList($where);
        $this->assign([
            'rows' => $rows,
            'title' => '直播列表',
            'pageHTML' => $rows->render(),
            'type' => self::$room_type,
        ]);
        return view();

    }



    public function record()
    {
        $where = [];
        if (!empty($_GET['room_name'])) $where['r.room_name|r.room_id'] = ['like','%'.trim(input('get.room_name')).'%'];
        if (!isEmpty($_GET['status'])) $where['a.status'] = input('get.status');
        if (!isEmpty($_GET['type'])) $where['a.type'] = input('get.type');
        if (!isEmpty($_GET['nick_name'])) $where['u.nick_name|u.uuid|u.phone|u.user_id'] = ['like','%'.trim(input('get.nick_name')).'%'];

        $model  = new OrderRecord();

        $rows = $model->getList($where);

        $this->assign([
            'rows' => $rows,
            'title' => '派单记录',
            'pageHTML' => $rows->render(),
        ]);
        return view();

    }

    public function del()
    {
        $this->_del('order_record');
    }


    /**
     * 改变推荐状态
     */
    public function cate_top()
    {

        $this->_change('room_category','top');

    }

    /**
     * 改变推荐状态
     */
    public function top()
    {
        $data = input();
        $result = Db::name('room')->update(['top'=>$data['val'],'update_time'=>time(),'room_id'=>$data['id']]);
        if($result !== false){
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }

    public function edit()
    {
        if(request()->isPost()){
            $data  = input('post.');

            $name = Db::name('room')
                ->where('room_name',$data['room_name'])
                ->where('room_id','<>',$data['id'])
                ->value('room_id');
            if ($name){
                $this->error('该昵称已存在');
            }

            //指定房主
            if ($data['user_id']){
                if (is_numeric($data['user_id'])){
                    $user_id = Db::name('users')->where('uuid',$data['user_id'])->value('user_id');
                    if (!$user_id){
                        $this->error('用户不存在');
                    }
                }else{
                    $user_id = dehashid($data['user_id']);
                    if (!is_numeric($user_id)){
                        $this->error('用户id错误');
                    }
                }
                $data['user_id'] = $user_id;
            }

            $model = new \app\common\logic\Room();
            $result = $model->saveChange($data);


            if($result !== false){
                $this->success('操作成功',url('index'));
            }
            $this->error($model->getError());
        }
        $cate = Db::name('room_background')->order('create_time DESC')->select();
        $data = (new Room())->detail(['room_id'=>input('id')]);
        $this->assign([
            'title' => '房间编辑',
            'cate' => array_key($cate,'bg_id'),
            'data'=>$data,
            'type' => self::$room_type,
        ]);
        return view();
    }

    public function order_edit()
    {
        if(request()->isPost()){
            $data  = input('post.');

            $name = Db::name('room')
                ->where('room_name',$data['room_name'])
                ->where('room_id','<>',$data['id'])
                ->value('room_id');
            if ($name){
                $this->error('该昵称已存在');
            }
            if ($data['user_id']){
                if (is_numeric($data['user_id'])){
                    $data['user_id'] = Db::name('users')->where('uuid',$data['user_id'])->value('user_id');
                }else{
                    $data['user_id'] = dehashid($data['user_id']);
                }
            }
            $data['type'] = 3;

            $model = new \app\common\logic\Room();



            $result = $model->saveChange($data);


            if($result !== false){
                $this->success('操作成功',url('order_index'));
            }
            $this->error($model->getError());
        }
        $cate = Db::name('room_background')->order('create_time DESC')->select();
        $data = (new Room())->detail(['room_id'=>input('id')]);
        $this->assign([
            'title' => '房间编辑',
            'cate' => array_key($cate,'bg_id'),
            'data'=>$data,
            'type' => self::$room_type,
        ]);
        return view();
    }

    public function change()
    {
        $data = input();
        $result = Db::name('room')->update(['status'=>$data['type'],'room_id'=>$data['id']]);
        if($result !== false){
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }

    public function delete()
    {
        $id     = input('id');
        Db::startTrans();
        try{
            Db::name('room')->delete($id);
            Db::name('gift_record')->where('room_id',$id)->update(['room_id'=>0]);
        }catch (\Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('删除成功');

    }


    /**
     * 房间分类标签
     */
    public function cate()
    {
        $where = [];
        if (!empty($_GET['cate_name']))$where['cate_name'] = ['like','%'.trim(input('get.cate_name')).'%'];
        if (!isEmpty($_GET['status']))$where['status'] = input('get.status');
        $model = new RoomCategory();
        $rows = $model->getList($where);
        $this->assign([
            'rows' => $rows,
            'pageHTML' => $rows->render(),
            'title' => '房间分类标签',
        ]);
        return view();
    }

    public function cate_edit()
    {
        $this->_edit('room_category','房间分类标签编辑',url('cate'),false);
        return view();
    }

    public function cate_change()
    {

        $this->_change('room_category');

    }

    public function cate_del()
    {
        $id     = input('id');
        $result = Db::name('play_category')->delete($id);
        if ($result !== false){
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }

    public function bg()
    {

        $model  = new RoomBackground();
        $rows = $model->getList();
        $this->assign([
            'rows' => $rows,
            'title' => '房间背景列表',
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }

    public function bg_edit()
    {
        $this->_edit('room_background','房间背景编辑',url('bg'),'room_background');
        return view();
    }


    public function bg_del()
    {
        $this->_del('room_background');
    }


    /**
     * 音乐列表
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     */
    public function music()
    {
        $model = new Music();
        $map = [];
        if (is_numeric(input('status'))){
            $map['status'] = input('status');
        }
        if (!empty(input('keyword'))){
            $map['singer|song_name'] = ['like','%'.trim(input('keyword')).'%'];
        }
        $rows = $model->_list($map);
        $this->assign([
            'rows' => $rows,
            'title' => '直播列表',
            'pageHTML' => $rows->render(),
        ]);
        return view();
    }

    public function music_edit()
    {

        if (is_numeric(session('user_id'))){
            $url = url('music');
        }else{
            $url = url();
        }

        $this->_edit('music','音乐编辑',$url,false);
        $auth  = new Auth(config('qiniu')['ACCESSKEY'], config('qiniu')['SECRETKEY']);
        $token = $auth->uploadToken(config('qiniu')['bucket']);
        $domain = config('qiniu')['domain'];
        $this->assign([
            'token'=>$token,
            'domain'=> $domain,

        ]);
        return view();
    }


    public function music_change()
    {

        $this->_change('music');

    }

    public function music_delete()
    {
        $this->_del('music');
    }




}